package example.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        //设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
        rabbitTemplate.setMandatory(true);

        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("ConfirmCallback:     " + "相关数据：" + correlationData);
                System.out.println("ConfirmCallback:     " + "确认情况：" + ack);
                System.out.println("ConfirmCallback:     " + "原因：" + cause);
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("ReturnCallback:     " + "消息：" + message);
                System.out.println("ReturnCallback:     " + "回应码：" + replyCode);
                System.out.println("ReturnCallback:     " + "回应信息：" + replyText);
                System.out.println("ReturnCallback:     " + "交换机：" + exchange);
                System.out.println("ReturnCallback:     " + "路由键：" + routingKey);
            }
        });
        return rabbitTemplate;
    }

    @Bean
    public Queue queueA() {
        return new Queue("rao.queue.aaa", true, false, false);
    }

    @Bean
    public DirectExchange exchangeA() {
        return new DirectExchange("rao.exchange.aaa", true, false);
    }

    @Bean
    public Binding bindingA(@Qualifier("queueA") Queue queue, @Qualifier("exchangeA") DirectExchange directExchange) {
        return BindingBuilder
                .bind(queue)
                .to(directExchange)
                .with("rao.route.aaa");
    }

    @Bean
    public Binding bindingB(@Qualifier("queueB") Queue queue, @Qualifier("exchangeB") DirectExchange directExchange) {
        return BindingBuilder
                .bind(queue)
                .to(directExchange)
                .with("rao.route.bbb");
    }

    @Bean
    public Binding bindingC(@Qualifier("queueC") Queue queue, @Qualifier("exchangeC") DirectExchange directExchange) {
        return BindingBuilder
                .bind(queue)
                .to(directExchange)
                .with("rao.route.ccc");
    }

    @Bean
    public Queue queueB() {
        return new Queue("rao.queue.bbb", true, false, false);
    }

    @Bean
    public DirectExchange exchangeB() {
        return new DirectExchange("rao.exchange.bbb", true, false);
    }

    @Bean
    public Queue queueC() {
        return new Queue("rao.queue.ccc", true, false, false);
    }

    @Bean
    public DirectExchange exchangeC() {
        return new DirectExchange("rao.exchange.ccc", true, false);
    }


}
